﻿ 
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using CarTrackr.Business;
using CarTrackr.Business.Services;


namespace CarTrackr.Controllers
{
    [Authorize]
    public class CarController : Controller
    {
        private IAppService service;

        private User currentUser;

        public CarController(): this(new AppService())
        {           
        }

        public CarController(IAppService appService)
        {
           service = appService;
        }

        //For test purpose 
        public CarController(IAppService appService,User user)
        {
            service = appService;

            currentUser = user;
        }


        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            currentUser = service.GetUserByName(HttpContext.User.Identity.Name);  

            
            base.OnActionExecuting(filterContext);
        }

        public ActionResult Index()
        {
             return RedirectToAction("List", "Car");
        }

        public ActionResult List()
        {
            List<Car> cars = service.ListCars(currentUser.UserId);
 
            return View("List", cars);
        }


        public ActionResult Details(string licensePlate)
        {
            Car car = service.GetCarByLicensePlate(licensePlate);

            car.Refuellings = service.ListRefuellings(car.Id);
          
            return View("Details", car);
        }

        public ActionResult Edit(string licensePlate)
        {
            Car car = service.GetCarByLicensePlate(licensePlate);

            return View("Edit", car);
        }

        [HttpPost]
        public ActionResult Edit(string licensePlate, FormCollection form)
        {
            Car car = service.GetCarByLicensePlate(licensePlate);

            UpdateModel(car, new[] { "Make", "Model", "PurchasePrice", "FuelType", "Description" });
            car.LicensePlate = form["NewLicensePlate"];


            if (car.IsValid)
            {
                   service.UpdateCar(car);
                   return RedirectToAction("Details", new { licensePlate = car.LicensePlate });
            }

            ModelState.UpdateWithViolation(car);
   
            car.LicensePlate = licensePlate;

            return View("Edit", car);
            
        }

      
        public ActionResult New()
        {
            return View("New", new Car());
        }

        [HttpPost]
        public ActionResult New(FormCollection form)
        {
            Car car = new Car();

            car.Make = form["Make"];
            car.Model = form["Model"];
            car.PurchasePrice = decimal.Parse(form["PurchasePrice"]);
            car.LicensePlate = form["LicensePlate"];
            car.FuelType  = form["FuelType"];
            car.Description = form["Description"];

     
            if (car.IsValid)
            {
                service.AddCar(car,currentUser.UserId);
                return RedirectToAction("Details", new { licensePlate = car.LicensePlate });
            }

            ModelState.UpdateWithViolation(car);
       
            return View("New", car);
        
        }

        public ActionResult Remove(string licensePlate)
        {
            Car car = service.GetCarByLicensePlate(licensePlate);

            service.RemoveCar(car.Id);

            return RedirectToAction("List", "Car");
        }


    }
}

 